package com.lzy.usermanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lzy.usermanager.pojo.User;
import com.lzy.usermanager.service.UserService;

@Controller
@RequestMapping(value="restful/user")
public class RestfulUserController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping(value="{userId}", method=RequestMethod.GET)
    public ResponseEntity<User> queryUserById(@PathVariable("userId") Long id){
        try {
            User user = userService.queryUserById(id);
            if(null == user){//如果没有查询到，资源不存在，响应404
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            //如果查询到，响应200
//            return ResponseEntity.status(HttpStatus.OK).body(user);
            return ResponseEntity.ok(user);
        } catch (Exception e) {  
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
    
    @RequestMapping(method=RequestMethod.POST)
    public ResponseEntity<Void> saveUser(User user){
        try {
            Boolean bool = userService.saveUser(user);
            if(bool){
                //新增成功
                return ResponseEntity.status(HttpStatus.CREATED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //新增失败，响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    
    @RequestMapping(method=RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(User user){
        try {
            Boolean bool = userService.updateUser(user);
            if(bool){
                //更新成功，响应204
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //更新失败，响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    

    @RequestMapping(method=RequestMethod.DELETE)
    public ResponseEntity<Void> deleteUser(@RequestParam(value="id", defaultValue="0") Long id){
        try {
            if(id == 0){//没有参数传递，即为默认值0。响应状态码400
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            Boolean bool = userService.deleteUser(id);
            if(bool){
                //删除成功，响应204
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //删除失败，响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
